﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MShop.Core.Domain.CartItem>>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm())
    { %>
    <div id="content" class="ovfl">
        <div>
            <img src='/images/step1.gif' alt="购物车流程" />
        </div>
        <div class="cart">
            <div>
                <h2>
                    我的购物车</h2>
                <% if (Model.Count() > 0)
                        { %>
                <table>
                    <tr>
                        <th>
                            商品编号
                        </th>
                        <th>
                            商品图片
                        </th>
                        <th>
                            商品名称
                        </th>
                        <th>
                            商品价格
                        </th>
                        <th>
                            赠送积分
                        </th>
                        <th>
                            商品数量
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                    <% foreach (var item in Model)
                       { %>
                    <tr>
                        <td>
                            <%=item.ProductId%>
                        </td>
                        <td>
                            <img class="thumb" src='<%=item.Product.ThumbImageUrl %>' alt='<%=item.ProductName %>' />
                        </td>
                        <td>
                            <%=Html.ActionLink(item.ProductName, "Item", "Product", new { id = item.ProductId }, new { @target = "_blank" })%>
                        </td>
                        <td>
                            <span class="price" id="totalPrice_<%=item.ProductId %>">¥<%=item.TotalPrice%>
                            </span>
                        </td>
                        <td class="point">
                            <span id="totalPoint_<%=item.ProductId %>">
                            <%=item.Product.Points *item.Quanitiy%>
                            
                            </span>
                        </td>
                        <td>
                            <a href="#this"  onclick="updateCartItem(<%=item.ProductId %>,<%=item.Quanitiy %>,-1,<%=item.Price %>,<%=item.Product.Points %>);">
                                <img src="/images/cart_icon1.gif" /></a>
                            <%=Html.TextBox("Quanitiy", item.Quanitiy, new { @class = "txt", @id = "Quanitiy_" + item.ProductId + "", onpaste = "return false;", onkeyup = "value=value.replace(/[^0-9_]/g,'')", style = "width:22px;text-align:center;" })%>
                            <a href="#this" onclick="updateCartItem(<%=item.ProductId %>,<%=item.Quanitiy %>,1,<%=item.Price %>,<%=item.Product.Points %>);">
                                <img src="/images/cart_icon2.gif" /></a>
                        </td>
                        <td>
                            <%: Html.ActionLink("删除", "Delete", new { id = item.Id }, new { @onclick = "return confirm('确定要从购物车删除该商品？');" })%>
                        </td>
                    </tr>
                    <% } %>
                </table>
                <table>
                    <tr>
                        <td class="noborder">
                            <input type="button" class="btn_login" value="继续购物" onclick="location.href='/';" />
                            <input type="submit" class="btn_login" value="清空购物车" onclick="return confirm('确实要清空购物车?');" />
                        </td>
                        <td class="noborder">
                            <input type="button" class="btn_login" value="结算中心" onclick="location.href='/trade/ShoppingCartSelect';" />
                        </td>
                    </tr>
                </table>
                <%}
                        else
                        { %>
                <p class="center">
                    <input type="button" class="btn_login" value="继续购物" onclick="location.href='/';" />
                </p>
                <%} %>
            </div>
        </div>
    </div>
    <%} %>
</asp:Content>
<asp:Content ID="m" ContentPlaceHolderID="cphMeta" runat="server">
    <script type="text/javascript"> 
        function modify(price, pid, point, step) {
            var obj = document.getElementById('Quanitiy_' + pid);
            if (obj.value == '') obj.value = 1;
            var value = parseInt(obj.value) + step;
            if (value <= 0) {
                obj.value = 1;
            }
            else {
                obj.value = value;
            }
            $('#totalPrice_' + pid).text((price * obj.value).toFixed(2));
            $('#totalPoint_' + pid).text((point * obj.value));
        }
        function updateCartItem(pid, quanitiy, step, price, point) {
            modify(price, pid, point, step);
            var obj = document.getElementById('Quanitiy_' + pid);
            quanitiy = obj.value;
            $.post('/trade/ModifyCartItem?pid=' + pid + '&quanitiy=' + quanitiy + '', null,
            function (result) {               
                if (result == 1) {
                  
                }
                else {
                    alert('网络出现故障，购物车更新不成功。');
                }
            }, "text");
        }
    </script>
</asp:Content>
